package com.fx.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fx.commonuntils.ReturnMessage;
import com.fx.commonuntils.ReturnMessageUtils;
import com.fx.eduservice.listner.SubjectExcelListener;
import com.fx.eduservice.entity.excel.SubjectData;
import com.fx.eduservice.entity.vo.EduSubject;
import com.fx.eduservice.mapper.EduSubjectMapper;
import com.fx.eduservice.response.subject.OneSubject;
import com.fx.eduservice.response.subject.RecursionSubject;
import com.fx.eduservice.response.subject.TwoSubject;
import com.fx.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-11-05
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public ReturnMessage<Object> saveSubject(MultipartFile multipartFile, EduSubjectService eduSubjectService) {
        try {
            //文件输入流
            InputStream in = multipartFile.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, SubjectData.class, new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public ReturnMessage<Object> getSubjectTree() {

        //查询所有的父节点课程分类信息

        //创建条件构造器
        QueryWrapper<EduSubject> fatherWrapper = new QueryWrapper<>();

        fatherWrapper.select("id", "title", "parent_id").eq("parent_id", 0);

        List<EduSubject> fatherSubjectsList = baseMapper.selectList(fatherWrapper);

        //查询所有子节点分析课程信息

        //创造条件构造器
        QueryWrapper<EduSubject> childWrapper = new QueryWrapper<>();
        childWrapper.select("id", "title", "parent_id").ne("parent_id", 0);
        List<EduSubject> childSubjectsList = baseMapper.selectList(childWrapper);

        //构造返回结果集，封装父节点信息
        //构造最终返回结果集合
        List<OneSubject> treeList = new ArrayList<>();


        //外层循环父节点信息
        for (EduSubject fatherSubject : fatherSubjectsList
        ) {
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(fatherSubject, oneSubject);
            //内层循环所有子节点
            for (EduSubject childSubject : childSubjectsList
            ) {

                if (fatherSubject.getId().equals(childSubject.getParentId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(childSubject, twoSubject);
                    oneSubject.getChildren().add(twoSubject);

                }
            }

            treeList.add(oneSubject);

        }


        return ReturnMessageUtils.success(treeList);
    }

    @Override
    public ReturnMessage<Object> getSubjectTreeById(EduSubject eduSubject) {


        //调用递归查询方法
        List<EduSubject> departmentDtos = getSubjectmentList(eduSubject);


        return ReturnMessageUtils.success(departmentDtos);
    }

    /**
     * 递归查询部门
     *
     * @param eduSubject
     * @return eduSubjects
     */
    private List<EduSubject> getSubjectmentList(EduSubject eduSubject) {

        //构造条件构造器
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "title", "parent_id").eq("parent_id", eduSubject.getParentId());
        List<EduSubject> eduSubjects = baseMapper.selectList(queryWrapper);
        if (eduSubjects.size() > 0) {
            EduSubject eduSubject1 = new EduSubject();
            for (int i = 0; i < eduSubjects.size(); i++) {
                eduSubject1.setParentId(eduSubjects.get(i).getId());
                List<EduSubject> dtos = getSubjectmentList(eduSubject1);
                eduSubjects.get(i).setChildernRecursion(dtos);
            }
        }
        return eduSubjects;
    }


}
